<?php
class SettingAction extends CAction{
	private $_model;
	
	public function run(){
		$model = $this->loadModel();
		if(isset($_POST['ajax']) && $_POST['ajax']==='userinfo-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST['Userinfo']))
		{
			$model->attributes=$_POST['Userinfo'];
			if($model->save()){
				$this->getController()->refresh();
			}
		}
		$this->getController()->render('setting',array('model'=>$model));
	}
	
	public function loadModel()
	{
		if($this->_model===null)
		{
			$this->_model = Userinfo::model()->find('uid=?' ,array(Yii::app()->user->id));
			if($this->_model===null){
				$this->_model = new Userinfo;
				$this->_model->user = User::model()->find('id=?' ,array(Yii::app()->user->id));
				if($this->_model->user === null)
					throw new CHttpException(404,'The requested page does not exist.');
			}
		}
		return $this->_model;
	}
}
?>